package controller

import (
	"danmu/common"
	"danmu/dto"
	"danmu/model"
	"danmu/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"net/http"
	"strconv"
)

//添加收藏
func Favorites(ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	vid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	if vid == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//查询视频是否存在
	var video = model.Video{}
	DB.First(&video,vid)
	if video.ID == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//验证是否已经收藏
	status := IsFavorites(uid.(uint),vid)
	if  status == 0{
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"已经收藏")
		return
	}else if status == -1{
		newFavorites := model.Interactive{
			Uid:uid.(uint),
			Vid:vid,
			IsFavorite:true,
		}
		DB.Create(&newFavorites)
	}else{
		var favorites = model.Interactive{}
		DB.Model(&favorites).Where("uid = ? AND vid = ?", uid, vid).Update("is_favorite", true)
	}
	//增加收藏数
	DB.Model(&video).UpdateColumn("favorites", gorm.Expr("favorites + 1"))
	response.Success(ctx,nil,"收藏成功")
}
//取消收藏
func Unfavorite (ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	vid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	if vid == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//查询视频是否存在
	var video = model.Video{}
	DB.First(&video,vid)
	if video.ID == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//验证收藏是否存在
	status := IsFavorites(uid.(uint),vid)
	if status != 0{
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"没有收藏")
		return
	}
	var favorites = model.Interactive{}
	DB.Model(&favorites).Where("uid = ? AND vid = ?", uid, vid).Update("is_favorite", false)
	//减收藏数
	DB.Model(&video).UpdateColumn("favorites", gorm.Expr("favorites - 1"))
	response.Success(ctx,nil,"取消收藏")
}
//获取收藏
func GetFavorites(ctx *gin.Context) {
	DB :=common.GetDB()
	var favorites []model.Interactive
	var count int
	uid,_ :=ctx.Get("id")
	page, _ := strconv.Atoi(ctx.Query("page"))
	pageSize, _ := strconv.Atoi(ctx.Query("page_size"))
	if page <= 0 && pageSize <= 0{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	DB = DB.Limit(pageSize).Offset((page - 1) * pageSize)
	DB.Where("uid = ? AND is_favorite = true", uid).Find(&favorites).Count(&count)
	response.Success(ctx,gin.H{"data":dto.ToFavoritesDto(favorites)},"获取成功")
}

//判断是否收藏
func GetInteractive(ctx *gin.Context) {
	uid,_ :=ctx.Get("id")
	var interactive = model.Interactive{}
	vid, _ := strconv.Atoi(ctx.Query("vid"))
	DB :=common.GetDB()
	//查询点赞收藏状态
	DB.Where("uid = ? AND vid = ?", uid, vid).Find(&interactive)
	response.Fail(ctx,gin.H{"favorites":interactive.IsFavorite,"like":interactive.IsLike},"没有收藏")
}

//是否已经收藏
func IsFavorites(uid uint,vid uint) int{
	//不存在返回-1，存在但是没有收藏返回1，已经收藏返回0
	DB :=common.GetDB()
	var favorites = model.Interactive{}
	DB.Where("uid = ? AND vid = ?", uid, vid).Find(&favorites)
	if favorites.ID == 0{
		return -1
	}else if favorites.IsFavorite == false{
		return 1
	}
	return  0
}

//点赞
func Like(ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	vid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	if vid == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	if vid == 0{
		response.Fail(ctx,nil,"视频不存在")
		return
	}
	//查询视频是否存在
	var video = model.Video{}
	DB.First(&video,vid)
	if video.ID == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//验证是否已经点赞
	status := IsLike(uid.(uint),vid)
	if  status == 0{
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"已经点赞")
		return
	}else if status == -1{
		newFavorites := model.Interactive{
			Uid:uid.(uint),
			Vid:vid,
			IsLike:true,
		}
		DB.Create(&newFavorites)
	}else{
		var favorites = model.Interactive{}
		DB.Model(&favorites).Where("uid = ? AND vid = ?", uid, vid).Update("is_like", true)
	}
	//增加收藏数
	DB.Model(&video).UpdateColumn("like_count", gorm.Expr("like_count + 1"))
	response.Success(ctx,nil,"点赞成功")
}
//取消赞
func Dislike (ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	vid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	if vid == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//查询视频是否存在
	var video = model.Video{}
	DB.First(&video,vid)
	if video.ID == 0 {
		response.Response(ctx,http.StatusBadRequest,400,nil,"视频不存在")
		return
	}
	//验证点赞是否存在
	status := IsLike(uid.(uint),vid)
	if status != 0{
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"没有点赞")
		return
	}
	var favorites = model.Interactive{}
	DB.Model(&favorites).Where("uid = ? AND vid = ?", uid, vid).Update("is_like", false)
	//减点赞数
	DB.Model(&video).UpdateColumn("like_count", gorm.Expr("like_count - 1"))
	response.Success(ctx,nil,"取消点赞")
}

func IsLike(uid uint,vid uint) int{
	//不存在返回-1，存在但是没有点赞返回1，已经点赞返回0
	DB :=common.GetDB()
	var like = model.Interactive{}
	DB.Where("uid = ? AND vid = ?", uid, vid).Find(&like)
	if like.ID == 0{
		return -1
	}else if like.IsLike == false{
		return 1
	}
	return  0
}